package com.example.simin_manage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.simin_manage.dto.LoginRequest;
import com.example.simin_manage.dto.MemberVO;
import com.example.simin_manage.dto.RegisterRequest;
import com.example.simin_manage.entity.SysDept;
import com.example.simin_manage.entity.SysUser;
import com.example.simin_manage.mapper.DeptMapper;
import com.example.simin_manage.mapper.SysUserMapper;
import com.example.simin_manage.util.HashUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: panpan
 * @time: 2025/9/14
 */
@Service
public class UserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private DeptMapper deptMapper;

    public SysUser login(LoginRequest request) {
        SysUser user = userMapper.selectOne(new QueryWrapper<SysUser>()
                .eq("username", request.getUsername())
                .eq("status", 1));

        if (user != null) {
            // 把用户输入的密码加密后，与数据库中的密码比对
            String inputPassword = HashUtil.sha256(request.getPassword());
            if (inputPassword.equals(user.getPassword())) {
                return user;
            }
        }
        return null;
    }
    public SysUser register(RegisterRequest request) {
        SysUser user = new SysUser();
        user.setUsername(request.getUsername());
        user.setPassword(HashUtil.sha256(request.getPassword())); // 用 sha256
        user.setNickname(request.getNickname());
        user.setRole("MEMBER"); // 这里固定是组员
        user.setStatus(1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        // 根据组长ID查部门
        SysDept dept = deptMapper.selectOne(
                new QueryWrapper<SysDept>().eq("leader_id", request.getLeaderId())
        );
        if (dept != null) {
            user.setDeptId(dept.getId());
        } else {
            throw new RuntimeException("组长未分配部门，无法注册员工");
        }

        userMapper.insert(user);
        return user;
    }

    // 查询某个组长的员工
    public List<MemberVO> getMembersByLeader(Long leaderId) {
        // 查部门
        SysDept dept = deptMapper.selectOne(
                new QueryWrapper<SysDept>().eq("leader_id", leaderId)
        );
        if (dept == null) return Collections.emptyList();

        // 查该部门的组员
        List<SysUser> users = userMapper.selectList(
                new QueryWrapper<SysUser>()
                        .eq("dept_id", dept.getId())
                        .eq("role", "MEMBER")

        );

        // 拼接部门名
        return users.stream().map(user -> {
            MemberVO vo = new MemberVO();
            vo.setId(String.valueOf(user.getId()));
            vo.setUsername(user.getUsername());
            vo.setNickname(user.getNickname());
            vo.setStatus(user.getStatus());
            vo.setDeptId(user.getDeptId());
            vo.setDailytarget(user.getDailyTarget());
            vo.setName(dept.getName());
            return vo;
        }).collect(Collectors.toList());
    }

    /** 查询单个用户 */
    public SysUser getUserById(Long id) {
        return userMapper.selectById(id);
    }
    /** 更新用户 */
    public SysUser updateUser(SysUser user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
        return userMapper.selectById(user.getId());
    }

    /** 删除用户 */
    public boolean deleteUser(Long id) {
        return userMapper.deleteById(id) > 0;
    }



}